//
//  ChaKanJieGuoViewController.swift
//  ZHM
//
//  Created by zzddongge on 16/9/11.
//  Copyright © 2016年 zzd. All rights reserved.
//

import UIKit

class ChaKanJieGuoViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    
    let screenWidth = UIScreen.main.bounds.width
    let screenHeight = UIScreen.main.bounds.height
    var chaKanChengJiTableView:UITableView?
    
    //定义一个完成按钮
    var finishBtn:UIButton?
    
    //定义一个变量,用于保存从上个页面传递过来的类别名称
    var moShiCategory:String?
    
    var miniteValue:Int = 0
    
    override func viewDidLoad() {
        
        let leftBarButton = UIButton(frame:CGRect(x: 0,y: 0,width: 20,height: 20))
        leftBarButton.setBackgroundImage(UIImage(named: "fanhui"), for: UIControlState())
        leftBarButton.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarButtonItem = UIBarButtonItem(customView: leftBarButton)
        self.navigationItem.leftBarButtonItems = [leftBarButtonItem]
        
        self.view.backgroundColor = UIColor.white
        self.title = "训练成绩"
        
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        //修改导航栏按钮颜色
        self.navigationController?.navigationBar.tintColor = UIColor.white
        self.navigationController?.navigationBar.barTintColor =
            UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1)
        
        //初始化UITableView
        chaKanChengJiTableView = UITableView(frame: CGRect(x: 0, y: 15, width: screenWidth, height: screenHeight))
        //设置代理和数据源
        self.chaKanChengJiTableView?.delegate = self
        self.chaKanChengJiTableView?.dataSource = self
        
        let cellNib = UINib(nibName: "ChaKanChengJiTableViewCell", bundle: nil)
        self.chaKanChengJiTableView!.register(cellNib, forCellReuseIdentifier: "chaKanChengJiTableViewCell")
        
        self.view.addSubview(chaKanChengJiTableView!)
        
        print(miniteValue)
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
     /*
     作者:张振东
     时间:2016-9-23
     功能:监听键盘消失的事件
     */
    override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self)
    }
    
    
    /*
     作者:张振东
     时间:2016-9-22
     功能:监听键盘即将隐藏时的事件
     */
    func handleKeyboardWillHide(_ notification:Notification){
        if ((finishBtn?.superview) != nil){
            finishBtn?.removeFromSuperview()
        }
    }

    /*
     作者:张振东
     时间:2016-9-12
     功能:返回几组
     */
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    
    /*
     作者:张振东
     时间:2016-9-12
     功能:每组显示几个cell
     */
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    /*
     作者:张振东
     时间:2016-9-12
     功能:定义tableViewcell的高度
     */
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: ChaKanChengJiTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chaKanChengJiTableViewCell") as! ChaKanChengJiTableViewCell!
        
        //接收传递过来的训练时间值
        let timerValue = UserDefaults.standard.integer(forKey: "timerValue")
        print(timerValue)
        
        let chaZhi = miniteValue*60-timerValue
        
        if moShiCategory == "计时"{
            if timerValue < 60{
                cell.xunLianShiJianZhi.text = "\(timerValue)"
                cell.fenZhong.text = "秒"
            }else{
                let fenZhongZhi = timerValue/60
                let miaoZhi = timerValue%60
                cell.xunLianShiJianZhi.text = "\(fenZhongZhi)"
                cell.fenZhong.text = "分钟\(miaoZhi)秒"
            }
        }else if moShiCategory == "定时"{
            if timerValue < 60{
                
                cell.xunLianShiJianZhi.text = "\(timerValue)"
                cell.fenZhong.text = "秒"
                
            }else{
                let fenZhongZhi = chaZhi/60
                let miaoZhi = chaZhi%60
                cell.xunLianShiJianZhi.text = "\(fenZhongZhi)"
                cell.fenZhong.text = "分钟\(miaoZhi)秒"
            }
        }
        
        //接收传递过来的正确率值
        let zhengQueLv = UserDefaults.standard.integer(forKey: "zhengQueLv")
        print(zhengQueLv)
        
        cell.zhengQueLvZhi.text = "\(zhengQueLv)%"
        
        //接收传递过来的速度值
        let xiaoJianPanLianXiSuDu = UserDefaults.standard.integer(forKey: "xiaoJianPanLianXiSuDu")
        print(xiaoJianPanLianXiSuDu)
        cell.suDuZhi.text = "\(xiaoJianPanLianXiSuDu)"
        
        return cell

    }
    
    
    //返回至上一个界面
    func backToPrevious(){
        self.navigationController?.popViewController(animated: true)
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
